<?php namespace simpleFlow\app;

use simpleFlow\error\ErrorException as SFErrorException;
use simpleFlow\control\ControlManager as SFControlManager;
use simpleFlow\Session as SFSession;

define('SF_DEBUG', true);
define('SF_DEBUG_LOG', false);
define('SF_MANAGER_UNLOCKED', false);
define('SF_VIEW_LIST_ROWS', 10);
define('SF_THEME', 'simpleFlow');
define('SF_ENV', 'dev');
define('SF_APP_DIR_ROOT', realpath(__CLASS__).DIRECTORY_SEPARATOR);
define('SF_APP_DIR_CONFIG', SF_APP_DIR_ROOT.'.configs'.DIRECTORY_SEPARATOR);
define('SF_THEME_DIR', SF_APP_DIR_ROOT.DIRECTORY_SEPARATOR.'.configs'.DIRECTORY_SEPARATOR.'themes'.DIRECTORY_SEPARATOR);
define('SF_LIB_DIR_ROOT', '/var/www/simpleflow/.simpleFlow/');
define('SF_VIRTUAL_HOST',true);
//define('SF_URL_ROOT', 'http://localhost/phpsimpleflow/'.SF_ENV); // if SF_VIRTUAL_HOST is true, clean this constant
define('SF_URL_ROOT', ''); // if SF_VIRTUAL_HOST is true, clean this constant

require_once(SF_LIB_DIR_ROOT.'includes.php');

if(SF_DEBUG)
{
	error_reporting(E_ALL);
	ini_set('display_errors', 'on');
}

$old_exception_error_handler	= set_exception_handler('simpleFlow\app\exception_handler');
$old_error_handler				= set_error_handler('simpleFlow\app\error_handler');

function getIP()
{
	$strIP = '';
	if(getenv("HTTP_CLIENT_IP")) { $strIP = getenv("HTTP_CLIENT_IP"); }
	elseif(getenv("HTTP_X_FORWARDED_FOR")) { $strIP = getenv("HTTP_X_FORWARDED_FOR"); }
	elseif(getenv("REMOTE_ADDR")) { $strIP = getenv("REMOTE_ADDR"); }
	else { $strIP = "UNKNOWN"; }
	return $strIP;
}
function exception_handler($objException) { echo $objException->getMessage()."\n"; }
function error_handler($intCode, $strMessage, $strFile, $intLine)
{
	$booLog = \SF_DEBUG_LOG ? true : false;
	if(!(error_reporting() & $intCode)) { return; }
	if(!ini_get("display_errors")) { return; }
	
	$strError = '<span style=\'background-color:khaki;font-size:.9em;\'>[<b>simpleFlow</b>] ';
	if($booLog)
	{
		$strIP = getIP();
		$strErrorLog = '['.\date('d/m/Y H:i:s').']'."\n";
		$strErrorLog.= 'ip='.$strIP.'|host1='.\gethostbyname($strIP).'|host2='.\gethostbyaddr($strIP)."\n";
		$strErrorLog.= "message={$strMessage}|file={$strFile}|line={$intLine}|code={$intCode}\n";
		$strErrorLog.= "phpversion=".PHP_VERSION."|phpos=".PHP_OS."\n";
	}
	switch($intCode)
	{
		case E_USER_ERROR:
			$strError.= "<i>ERROR</i> [<b>{$intCode}</b>] {$strMessage}<br />\n";
			$strError.= "  Fatal error on line {$intLine} in file {$strFile}";
			$strError.= ", PHP ".PHP_VERSION." (".PHP_OS.")<br />\n";
			$strError.= "Aborting...<br />\n";
			if($booLog)
			{
				$strErrorLog.= "type=ERROR\n";
			}
			exit(1);
			break;

		case E_USER_WARNING:
			$strError.= "<i>WARNING</i> [<b>{$intCode}</b>] {$strMessage}<br />\n";
			if($booLog)
			{
				$strErrorLog.= "type=WARNING\n";
			}
			break;

		case E_USER_NOTICE:
			$strError.= "<i>NOTICE</i> [<b>{$intCode}</b>] {$strMessage}<br />\n";
			if($booLog)
			{
				$strErrorLog.= "type=NOTICE\n";
			}
			break;

		default:
			$strError.= "Unknown error type: [<b>{$intCode}</b>] ${$strMessage} [file:{{$strFile}} ({$intLine})]<br />\n";
			break;
	}
	
	echo $strError.'</span><pre>';
	if($booLog)
	{
		error_log($strErrorLog."\n",3,\SF_APP_DIR_ROOT.'.log'.DIRECTORY_SEPARATOR.SF_ENV.'.ini');
	}
	return true;
}

SFSession::init();
SFControlManager::start($_GET);

?>